home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / PickMeUp.sit / Pick Me Up / source code / Movie app source / pickmeUp97 / source / scaleDrawRect.cp < prev    next >
Text File  |  1997-06-27  |  4KB  |  116 lines

  1. //===========================================================================
  2. // ••  ScaleDrawRect
  3. //===========================================================================
  4. void                        
  5. LPictThumbView::ScaleDrawRect(    Rect  inOriginalRect, Rect  inThumnailRect,
  6.                                                 Rect*  outScaledRect)
  7. {
  8.     /*
  9.         NOTES:
  10.         This routine is for scaling the drawRect of a picture to fit into
  11.         a thumbnail sized drawing area.  For example, if you have a picture
  12.         that is 500 x 700 pixels, but only have a 70 x 70 pixel size
  13.         area in which to draw the image, this routine will scale
  14.         the picture down.
  15.         
  16.         This routine will also scale a rect(outScaledRect) so that it will center
  17.         itself in the thumnail.  This way, the picture that is scaled down will
  18.         be centered when it is drawn in the thumbnail.
  19.         
  20.         Why only Rects?
  21.         Generally when drawing pictures, the system routines that do the actual 
  22.         drawing want a Rect to define the size and location of the picture in the
  23.         current GrafPort.  that is why all that is needed here is to scale the 
  24.         rect and then pass outScaledRect to the draw routine.
  25.         
  26.         Rect    inOriginalRect;      Input the original size of the picture's rect here.
  27.         Rect    inThumbnailRect;    Input the size of the thumnail where the picture is
  28.                                     supposed to be drawn here.
  29.         Rect*    outScaledRect;        This is where the scaled rect for the picture
  30.                                     will be passed back to you.
  31.                                     
  32.     
  33.     
  34.     */
  35.     short    percent = 0;
  36.     ::InsetRect(&inThumnailRect, 1,1);
  37.     
  38.     //  Getting the h and w of the orig pics frame size.
  39.     short w = inOriginalRect.right - inOriginalRect.left;
  40.     short h = inOriginalRect.bottom - inOriginalRect.top;
  41.             
  42.     //  Getting the h and w of the thumbs frame size.
  43.     short Aw = inThumnailRect.right - inThumnailRect.left;
  44.     short Ah = inThumnailRect.bottom - inThumnailRect.top;
  45.             
  46.     //  Comparing which is bigger with the orig pics
  47.     //  Why do we have to compare?  In order to scale the picture, we
  48.     //  need to know which side is bigger.  That way, depending on 
  49.     //  which one is bigger, the bigger side will be the full size
  50.     //  of the thumbnail window, and then we'll scale the other
  51.     //  side down to match the other side.
  52.     if(w > h)
  53.     {
  54.         //  Since the width is larger than the height, we are here.
  55.                 
  56.         //  We first set the left of inOriginalRect to inThumbnailRects left, 
  57.         //  plus some offset.  inThumnailRect was inset by 1, and the offset
  58.         //  is just found by experimenting.
  59.         inOriginalRect.left = inThumnailRect.left + 1;
  60.                 
  61.         //  Since the width is bigger than the height, we set the 
  62.         //  width to be the size of inThumnailRects width.  That way the
  63.         //  entire width will fit into the thumbnail rect.
  64.         inOriginalRect.right = Aw;
  65.                 
  66.         //  Now we need to know what percentage the image was scaled down. 
  67.         //  We need to know this because we need to scale the other side
  68.         //  just as much as we scaled the the width.
  69.         percent = ((Aw * 100)/w);
  70.                 
  71.         //  Now we take the percent and determine a pixel amount based on the
  72.         //  percentage we scaled.
  73.         inOriginalRect.bottom = ((percent * h)/100);
  74.                 
  75.         //  Now we need to center the newly scaled rect in the thumbnail
  76.         //  rect.  If we didn't do this, then the upper left corner of the
  77.         //  image would be at the upper left corner of the thumbnail.  We want
  78.         //  the image to be in the middle of the thumbnail so we kluge here.
  79.         inOriginalRect.top = ((Ah - inOriginalRect.bottom)/2);
  80.                 
  81.         //  Just some fudging to get the centering just right.  This is found
  82.         //  with experimentation.
  83.         inOriginalRect.top += 1;
  84.                 
  85.         //  Since we just collapsed the rect, top down, we need to expand the 
  86.         //  bottom of the rect as much as we collapsed it from top.
  87.         inOriginalRect.bottom += inOriginalRect.top;
  88.                 
  89.                 
  90.     }else if(w < h)
  91.         {
  92.                     
  93.             inOriginalRect.top = inThumnailRect.top + 1;
  94.             inOriginalRect.bottom = Ah;
  95.             percent = ((Ah * 100)/h);
  96.                     
  97.             inOriginalRect.right = ((percent * w)/100);
  98.             inOriginalRect.left = ((Aw - inOriginalRect.right)/2);
  99.             inOriginalRect.left += 2;
  100.             inOriginalRect.right += inOriginalRect.left;
  101.         }else if(w == h)
  102.             {
  103.                 InsetRect(&inThumnailRect, 1,1);
  104.                 inOriginalRect = inThumnailRect;
  105.                     
  106.             }
  107.             
  108.         //  Now passing the scaled rect back into outScaledRect
  109.         ::BlockMoveData(&inOriginalRect, outScaledRect, sizeof(Rect));
  110.         return;
  111.  
  112.  
  113.  
  114.  
  115. }
  116.